package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class OrganizationOptionDialogFragment extends DialogFragment implements View.OnClickListener{

    @InjectView(R.id.organization_invite)
    TextView mOrganizationInvite;
    @InjectView(R.id.organization_apply)
    TextView mOrganizationApply;
    @InjectView(R.id.cancel)
    TextView mCancel;
    @InjectView(R.id.invite_layout)
    LinearLayout mInviteLayout;


    private static OrganizationOptionDialogFragment newInstance(boolean showInvite) {
        OrganizationOptionDialogFragment mOrganizationOptionDialogFragment = new OrganizationOptionDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(Const.SHOW_INVITE, showInvite);
        mOrganizationOptionDialogFragment.setArguments(bundle);
        return mOrganizationOptionDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        boolean showInvite = getArguments().getBoolean(Const.SHOW_INVITE, true);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
        View view = inflater.inflate(R.layout.dialog_organization_option, container, false);
        ButterKnife.inject(this, view);
        mCancel.setOnClickListener(this);
        if(!showInvite){
            mInviteLayout.setVisibility(View.GONE);
        }
        mOrganizationInvite.setOnClickListener(this);
        mOrganizationApply.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.organization_invite:
                if(mOrganizationOptionCallback != null){
                    mOrganizationOptionCallback.onOrganizationInvite();
                }
                break;
            case R.id.organization_apply:
                if(mOrganizationOptionCallback != null){
                    mOrganizationOptionCallback.onOrganizationApply();
                }
                break;
            case R.id.cancel:
                break;

            default:
                break;
        }
        getDialog().dismiss();
    }

    public static interface OrganizationOptionCallback{
        void onOrganizationInvite();
        void onOrganizationApply();
    }

    private void setOrganizationOptionCallback(OrganizationOptionCallback organizationOptionCallback){
        this.mOrganizationOptionCallback = organizationOptionCallback;
    }

    private OrganizationOptionCallback mOrganizationOptionCallback;

    public static class OrganizationOptionDialogBuilder {

        OrganizationOptionDialogFragment mCourseContentDialogFragment;
        private String mTag;

        public OrganizationOptionDialogBuilder(){
            mCourseContentDialogFragment = OrganizationOptionDialogFragment.newInstance(true);
            mTag = "organization_option";
        }

        public OrganizationOptionDialogBuilder(boolean showInvite){
            mCourseContentDialogFragment = OrganizationOptionDialogFragment.newInstance(showInvite);
            mTag = "organization_option";
        }


        public OrganizationOptionDialogBuilder setOrganizationCallback(OrganizationOptionCallback organizationCallback){
            mCourseContentDialogFragment.setOrganizationOptionCallback(organizationCallback);
            return this;
        }

        public OrganizationOptionDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
            fragmentTransaction.add(mCourseContentDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
